package com.example.carrierapp.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.carrierapp.bean.waybill.WaybillListBean;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.GlideApp;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.util.RoundCornerTransform;
import com.example.lb.fingerdemo.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WaybillArriveNoAdatpter extends XRecyclerView.Adapter<WaybillArriveNoAdatpter.ViewHolder> {
    private List<WaybillListBean.WaybillListInfo> mList = new ArrayList();
    private int mClickIndex;
    private final RoundCornerTransform mRoundedCorners;

    public WaybillArriveNoAdatpter(Context context) {
        mRoundedCorners = new RoundCornerTransform(DisplayUtil.dip2px(context, 4.0f));
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_waybill_arriveno, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WaybillListBean.WaybillListInfo info = mList.get(position);
        holder.bindData(info, position,mRoundedCorners);

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void addDataNotClear(List<WaybillListBean.WaybillListInfo> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addData(List<WaybillListBean.WaybillListInfo> list) {
        if (list != null && list.size() > 0) {
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void updataItemData(WaybillListBean.WaybillListInfo bean) {
        mList.remove(mClickIndex);
        notifyItemRemoved(mClickIndex);

        mList.add(mClickIndex, bean);
        notifyItemInserted(mClickIndex);

        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public interface OnBtnClickListener {
        void onRevokeClick(WaybillListBean.WaybillListInfo info);

        void onModifyClick(WaybillListBean.WaybillListInfo info);

        void onDetailClick(WaybillListBean.WaybillListInfo info);
    }

    private OnBtnClickListener mOnItemClickListener;

    public void setOnBtnClickListener(OnBtnClickListener listener) {
        mOnItemClickListener = listener;
    }


    public class ViewHolder extends XRecyclerView.ViewHolder {
        @BindView(R.id.iv_waybill_pic)
        ImageView mIvPic;
        @BindView(R.id.tv_waybill_safe)
        TextView mTvSafe;
        @BindView(R.id.tv_waybill_address_start)
        TextView mTvAddressStart;
        @BindView(R.id.tv_waybill_address_end)
        TextView mTvAddressEnd;
        @BindView(R.id.tv_waybill_home_code)
        TextView mTvCode;
        @BindView(R.id.tv_detail_phone)
        TextView mTvPhone;
        @BindView(R.id.tv_waybill_carnumber)
        TextView mTvCarNumber;
        @BindView(R.id.tv_waybill_goods)
        TextView mTvGoods;
        @BindView(R.id.tv_waybill_person)
        TextView mTvPerson;

        @BindView(R.id.tv_waybill_state)
        TextView mTvState;
        @BindView(R.id.tv_waybill_detail)
        TextView mTvDetail;
        @BindView(R.id.tv_waybill_modify)
        TextView mTvModifyVolume;

        @BindView(R.id.tv_waybill_um_des)
        TextView mTvUnDes;
        @BindView(R.id.tv_waybill_um_revoke)
        TextView mTvRevoke;
        @BindView(R.id.tv_state_error)
        TextView mTvErrorDes;
        @BindView(R.id.con_stute_ing)
        ConstraintLayout mConsIng;
        @BindView(R.id.rl_stute_un)
        RelativeLayout mRlUn;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(WaybillListBean.WaybillListInfo info, int position, RoundCornerTransform mRoundedCorners) {
            if (info == null) {
                LogUtil.d("info is null");
                return;
            }

            mTvAddressStart.setText(info.pickCityName);
            mTvAddressEnd.setText(info.takeCityName);
            mTvCode.setText(info.code);
            DisplayUtil.setTextOrError(mTvPhone,info.driverPhone,"暂无电话");
            DisplayUtil.setTextOrError(mTvCarNumber,info.carNum,"暂无车牌");
            double volume=0;
            mTvSafe.setVisibility("已投保".equals(info.insureStateName) ? View.VISIBLE : View.GONE);
            if(info.loadingNum>0)
            {
                volume=info.loadingNum;
            }else if(info.shipment>0)
            {
                volume=info.shipment;
            }else  if(info.dispatchNum>0)
            {
                volume=info.dispatchNum;
            }
            mTvGoods.setText(info.proName + " 总" + volume+ info.unitName);
            mTvPerson.setText(info.driverName);
            GlideApp.with(itemView).load(info.productPicPath).placeholder(R.mipmap.icon_default_img).transform(WaybillArriveNoAdatpter.this.mRoundedCorners).into(mIvPic);

//            运单状态 = ['0'已派单, '2'已装货, '4'已送达, '5'外部数据, '-1' 已撤销],
            if (info.state == 0) {

                mConsIng.setVisibility(View.GONE);
                mTvErrorDes.setVisibility(View.GONE);
                mRlUn.setVisibility(View.VISIBLE);
                mTvState.setText("待装车");
                mTvState.setBackground(itemView.getResources().getDrawable(R.drawable.shape_empty_3483ff_3));
                mTvState.setTextColor(Color.parseColor("#3483ff"));
                mTvUnDes.setText(info.dueDateTip);
            } else if (info.isAbnormal)//異常
            {
                mConsIng.setVisibility(View.GONE);
                mRlUn.setVisibility(View.GONE);

                mTvErrorDes.setVisibility(View.VISIBLE);
                mTvErrorDes.setText(info.abnormalReason);
                mTvState.setText("异常");

                mTvState.setBackground(itemView.getResources().getDrawable(R.drawable.shape_empty_ff7848_2));
                mTvState.setTextColor(Color.parseColor("#ff7848"));
            } else//运输中
            {
                mTvState.setText("已装车");
                mConsIng.setVisibility(View.VISIBLE);
                mTvErrorDes.setVisibility(View.GONE);
                mRlUn.setVisibility(View.GONE);
                if (info.payState > 1&&info.payState!=5) {
                    mTvModifyVolume.setVisibility(View.GONE);
                }
                mTvState.setBackground(itemView.getResources().getDrawable(R.drawable.shape_empty_ff7848_2));
                mTvState.setTextColor(Color.parseColor("#ff7848"));
            }

            mIvPic.setOnClickListener(v -> {
                ActivityUtil.startSeePhoto(itemView.getContext(),info.productPicPath);
            });
            mTvModifyVolume.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onModifyClick(info);
                    mClickIndex = position;
                }
            });
            mTvRevoke.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onRevokeClick(info);
                }
            });
            mTvDetail.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onDetailClick(info);
                }
            });
        }
    }
}
